import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {
    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
           lock.lock();
            try {
                System.out.println("线程1加锁!");
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("线程1解锁!");
            }
        });
        thread1.start();
        Thread thread2 = new Thread(()->{
           lock.lock();
            try {
                System.out.println("线程2加锁!");
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("线程2解锁!");
            }
        });
        thread2.start();
    }
}
